package com.btex.app.treaty.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.btex.app.Config;
import com.btex.app.MyApplication;
import com.btex.app.R;
import com.btex.app.entity.FuturesListInfo;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.SharedPreferencesUtil;
import com.btex.app.tools.Tools;
import com.btex.app.treaty.HistoryAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/10/15
 * @time 下午6:05
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */

public class HistoryDialog extends Dialog {


    @BindView(R.id.history_list_dialog)
    RecyclerView mHistoryListDialog;
    @BindView(R.id.history_list_dialog_quxiao)
    TextView mHistoryListDialogQuxiao;
    private HistoryAdapter mHistoryAdapter;
    private List<FuturesListInfo.DataBean> mMData;
    private getValueLIstener mGetValueLIstener;

    public HistoryDialog(@NonNull Context context) {
        super(context, R.style.coupon_style);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_dialog);
        ButterKnife.bind(this);

        mHistoryListDialogQuxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        if (mMData == null) {
            Log.i("iweuo23ufo2u8f", "1");
            getMxCcontracts();
        }

        Log.i("iweuo23ufo2u8f", "2");
        initRecy();
//

        int viewHeight = Tools.getViewHeight(mHistoryListDialog);
        int viewHeight1 = Tools.getViewHeight(mHistoryListDialogQuxiao);
        Log.i("wejfkwefw", viewHeight + "==========" + viewHeight1);

        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (int) (widthPixels * 1);
        if (viewHeight == 0 || viewHeight1 == 0) {
            attributes.height = (int) (heightPixels * 0.56);
        } else {
            attributes.height = (int) (viewHeight + viewHeight1);
        }

        //让Dialog位置居中
        attributes.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);


    }

    private void initRecy() {


        mHistoryAdapter = new HistoryAdapter();
        mHistoryListDialog.setLayoutManager(new LinearLayoutManager(getContext()));
        mHistoryListDialog.setAdapter(mHistoryAdapter);
        mHistoryAdapter.setNewData(mMData);

        if (mHistoryAdapter != null) {
            mHistoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Log.i("welfwefijowj", "1");
                    FuturesListInfo.DataBean dataBean = (FuturesListInfo.DataBean) adapter.getData().get(position);
                    String contract_id = dataBean.getContract_id();
                    String pair = dataBean.getPair();
                    Config.CONTRACT_ID = contract_id;
                    Config.CONTRACT_ID_TYPE = pair;
                    if (mGetValueLIstener != null) {
                        mGetValueLIstener.get(pair, contract_id);
                    }

                    EventBus.getDefault().post("HistoryDialog");
                    HistoryDialog.this.dismiss();
                }
            });
        } else {
        }


    }


    private void getMxCcontracts() {


        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(getContext());
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(getContext(), sharedPreferences);
        RequestParams params = new RequestParams();
        params.addHeader("app-version", Config.VERSION);
        params.addHeader("channel", MyApplication.mApp.getApplicationMetaData("CXD_CHANNEL_ID"));
        params.addHeader("my-client", "android");

        int num = (int) ((Math.random() * 9 + 1) * 100000);
        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.GET_MX_CONTRACTS + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Log.i("fnmnrejhrej", responseInfo.result);

                try {

                    JSONObject jsonObj = new JSONObject(responseInfo.result);
                    if (jsonObj.getString("code").equals("1")) {

                        setList(responseInfo.result);

                    } else {

                    }


                } catch (JSONException e) {
                    e.printStackTrace();

                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }

        });

    }

    private void setList(String result) {

        Gson gson = new Gson();
        FuturesListInfo futuresInfo = gson.fromJson(result, FuturesListInfo.class);
        List<FuturesListInfo.DataBean> data = futuresInfo.getData();

        if (mHistoryAdapter != null && mHistoryListDialog != null) {
            mHistoryAdapter.setNewData(data);
            mHistoryListDialog.setAdapter(mHistoryAdapter);
        }


        if (mHistoryAdapter != null) {
            mHistoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                    FuturesListInfo.DataBean dataBean = (FuturesListInfo.DataBean) adapter.getData().get(position);
                    String contract_id = dataBean.getContract_id();
                    String pair = dataBean.getPair();
                    Config.CONTRACT_ID = contract_id;
                    Config.CONTRACT_ID_TYPE = pair;

                    if (mGetValueLIstener != null) {
                        mGetValueLIstener.get(pair, contract_id);
                    }

                    EventBus.getDefault().post("HistoryDialog");
                    HistoryDialog.this.dismiss();
                }
            });
        } else {

        }
    }


    public void setData(List<FuturesListInfo.DataBean> mData) {

        mMData = mData;
    }


    public interface getValueLIstener {

        void get(String data, String Contract_id);
    }


    public void getValue(getValueLIstener getValueLIstener) {


        this.mGetValueLIstener = getValueLIstener;
    }

}
